package link.ten_fei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import link.ten_fei.Bean.Pl_List;
import link.ten_fei.util.ConnectionFactory;

public class AddMRDaoImpl implements AddMRDao {

	@Override
	public int save(Pl_List list) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		int num = 0;
		try {
			// 2.键入sql语句
			String sql = "INSERT INTO pl_list (pl_number,pl_date,pl_name,pl_sex,pl_age,pl_expensetype,pl_diagnosis,pl_department,pl_rp,pl_doctor,pl_deployment,pl_review,pl_amount) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
			// 3.获取连接通路
			preparedStatement = connection.prepareStatement(sql);
			// 4.执行索引
			preparedStatement.setString(1, list.getPl_number());
			preparedStatement.setString(2, list.getPl_date());
			preparedStatement.setString(3, list.getPl_name());
			preparedStatement.setString(4, list.getPl_sex());
			preparedStatement.setString(5, list.getPl_age());
			preparedStatement.setString(6, list.getPl_expensetype());
			preparedStatement.setString(7, list.getPl_diagnosis());
			preparedStatement.setString(8, list.getPl_department());
			preparedStatement.setString(9, list.getPl_rp());
			preparedStatement.setString(10, list.getPl_doctor());
			preparedStatement.setString(11, list.getPl_deployment());
			preparedStatement.setString(12, list.getPl_review());
			preparedStatement.setString(13, list.getPl_amount());
			// 5.执行sql语句,进行结果添加到数据库中,返回值为更新影响行数
			num = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, null);
		}
		return num;
	}

	@Override
	public int change(Pl_List list,int id) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		try {
			// 2.键入sql语句
			String sql = "UPDATE pl_list set pl_number=?,pl_date=?,pl_name=?,pl_sex=?,pl_age=?,pl_expensetype=?,pl_diagnosis=?,pl_department=?,pl_rp=?,pl_doctor=?,pl_department=?,pl_review=?,pl_amount=? where id=?";
			// 3.获取连接通路
			preparedStatement = connection.prepareStatement(sql);
			// 4.执行索引
			preparedStatement.setString(1, list.getPl_number());
			preparedStatement.setString(2, list.getPl_date());
			preparedStatement.setString(3, list.getPl_name());
			preparedStatement.setString(4, list.getPl_sex());
			preparedStatement.setString(5, list.getPl_age());
			preparedStatement.setString(6, list.getPl_expensetype());
			preparedStatement.setString(7, list.getPl_diagnosis());
			preparedStatement.setString(8, list.getPl_department());
			preparedStatement.setString(9, list.getPl_rp());
			preparedStatement.setString(10, list.getPl_doctor());
			preparedStatement.setString(11, list.getPl_deployment());
			preparedStatement.setString(12, list.getPl_review());
			preparedStatement.setString(13, list.getPl_amount());
			preparedStatement.setInt(14, id);
			// 5.执行sql语句,进行结果更新到数据库中,返回值为更新影响行数
			int flag= preparedStatement.executeUpdate();
			return flag;
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, null);
		}
		return 0;
	}
}
